package com.younion.dao;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.younion.common.base.BaseDAO;
import com.younion.common.base.BaseVO;
import com.younion.common.exception.BaseException;
import com.younion.common.paginator.Paginator;
import com.younion.vo.API_AdvertisingVO;
import com.younion.vo.AdvertisingVO;

public class AdvertisingDAO extends BaseDAO {

	protected static final Log logger = LogFactory.getLog(AdvertisingDAO.class);

	public AdvertisingVO selectAdvertisingById(Map paramMap)
			throws BaseException {
		try {
			return (AdvertisingVO) this.getSqlMapClientTemplate()
					.queryForObject("t_advertising.selectById", paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					"AdvertisingDAO，调用selectAdvertisingById(Map paramMap)方法出错");
		}
	}

	public int selectMaxTop() throws BaseException {
		try {
			Object obj = this.getSqlMapClientTemplate().queryForObject(
					"t_advertising.selectMaxTop");
			if (obj == null)
				return 0;
			return ((Integer) obj).intValue();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	public boolean updateAdvertisingByMap(Map paramMap) throws BaseException {
		try {
			this.update("t_advertising.updateByMap", paramMap);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					"AdvertisingDAO，调用updateAdvertisingByMap(Map paramMap)方法出错");
		}
	}

	public boolean deleteAdvertisingById(Map paramMap) throws BaseException {
		try {
			this.getSqlMapClientTemplate().delete("t_advertising.deleteById",
					paramMap);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					"AdvertisingDAO，调用deleteAdvertisingById(Map paramMap)方法出错");
		}
	}

	public boolean deleteAdvertisingByIds(List list) throws BaseException {
		try {
			this.getSqlMapClientTemplate().delete("t_advertising.deleteByIds",
					list);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					"AdvertisingDAO，调用deleteAdvertisingByIds(List list)方法出错");
		}
	}

	/**
	 * 查询记录数
	 * 
	 * @param paramMap
	 * @return
	 * @throws BaseException
	 */
	public int selectCountByMap(Map paramMap) throws BaseException {
		if (paramMap == null)
			return 0;
		try {
			return queryCount("t_advertising.selectByMapCount", paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					"AdvertisingDAO，调用selectByMap(Map paramMap)方法出错");
		}
	}

	/**
	 * 实现翻页
	 * 
	 * @param condition
	 *            condition{Vo:条件VO对象，curPage:当前页面数，pageSize:每页最大行数}
	 * @return type : Page 返回一个分页的List，操作失败返回null
	 */
	public List selectForPage(Map condition, Paginator paginator)
			throws BaseException {
		if (condition == null)
			return null;
		int pageIndex = paginator.getStartIndex();
		int perPageSize = paginator.getPageSize();
		String sqlStr = "";
		try {
			Map vo = (Map) condition.get("paramMap");
			if (vo == null) {
				sqlStr = "t_advertising.selectAll";
			} else {
				sqlStr = "t_advertising.selectByMap";
			}
			return queryForPage(sqlStr, vo, pageIndex, perPageSize);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					"AdvertisingDAO，调用selectForPage（Map condition）方法出错");
		}
	}

	/**
	 * 向数据库中插入一个VO对象
	 * 
	 * @param advertisingVO
	 *            对象对应到数据库中的一条记录
	 * @return type : int 返回ID
	 */
	public Integer insertByVo(AdvertisingVO advertisingVO) throws BaseException {
		try {
			return (Integer) this.getSqlMapClientTemplate().insert(
					"t_advertising.insertByVo", (BaseVO) advertisingVO);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					" AdvertisingDAO，调用insertByVo(AdvertisingVO advertisingVO)方法出错");
		}
	}

	public List<AdvertisingVO> selectByIds(List list) throws BaseException {
		try {
			return this.getSqlMapClientTemplate().queryForList(
					"t_advertising.selectByIds", list);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					" AdvertisingDAO，selectByIds(List list)方法出错");
		}
	}

	public void updateStateByIds(Map map) throws BaseException {
		try {
			this.getSqlMapClientTemplate().update(
					"t_advertising.updateStateByIds", map);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					" AdvertisingDAO，updateStateByIds(Map map)方法出错");
		}
	}

	public void updateTopById(Map map) throws BaseException {
		try {
			this.getSqlMapClientTemplate().update(
					"t_advertising.updateTopById", map);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					" AdvertisingDAO，updateTopById(Map map)方法出错");
		}
	}

	public List<API_AdvertisingVO> getApiAdvertisement(Map paramMap)
			throws BaseException {
		try {
			return queryForList("t_advertising.selectApiAdvertisement", paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					" AdvertisingDAO，调用getApiAdvertisement(Map paramMap)方法出错");
		}
	}
	
public Collection selectByMap(Map map) throws BaseException{
		
		try{
			return this.queryForList("t_advertising.selectByMap", map);
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
		}

}
